package com.client.elements.normal.boxes;

import com.client.services.GreetingService;
import com.client.services.GreetingServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class NormalTextBox{

	/**Create a remote service proxy to talk to the server-side Greeting service.*/
	private final static GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

	public static Widget normalTextBox(){
		final TextBox nameField = new TextBox();
		nameField.setText("Max Mustermann");
		final Label errorLabel = new Label();
		final HTML anweisung = new HTML("<b>Bitte Name eingeben</b>");

		FlowPanel panel = new FlowPanel();
		panel.add(anweisung);
		panel.add(nameField);
		panel.add(errorLabel);

		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Datenverarbeitung");
		final Button closeButton = new Button("Schliessen");
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
		final HTML textToServerLabel = new HTML();
		final HTML serverResponseLabel = new HTML();

		VerticalPanel panelPopup = new VerticalPanel();
		panelPopup.add(textToServerLabel);
		panelPopup.add(serverResponseLabel);
		panelPopup.add(closeButton);
		dialogBox.setWidget(panelPopup);
		// Add a handler to send the name to the server
		nameField.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				if (nameField.getValue().length()<4) {
					nameField.addStyleName("red_Background");
				}else {
					nameField.removeStyleName("red_Background");
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
						sendNameToServer();
					}
				}	
			}

			/**Send the name from the nameField to the server and wait for a response. */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				final String textToServer = nameField.getText();

				// Then, we send the input to the server.
				textToServerLabel.setHTML("<b>Name:</b> " + textToServer);
				serverResponseLabel.setText("");

				greetingService.greetServer(textToServer, new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						dialogBox.setText("Datenverarbeitung - Fehler");
						serverResponseLabel.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML("Server error");
						dialogBox.center();
					}
					public void onSuccess(String result) {
						dialogBox.setText("Datenverarbeitung");
						serverResponseLabel.removeStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML("<b>Antwort des Servers: </b>" + result);
						dialogBox.center();
					}
				});
			}

		});
		return panel;
	}
}